#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>

unsigned long va;

static int vmmcall(int eax, int ebx, int ecx, int edx, int edi, int esi)
{
	int x = 10;
	
	__asm__ __volatile__ (
		".byte 0x0F,0x01,0xC1"
		:"=a"(x)
		:"a"(eax),
		"b"(ebx), "c"(ecx), "d"(edx), "D"(edi), "S"(esi) );

	return eax;
}


int init_module(char * tag, int rate)
{
	int ret;
	//printf("Load vmmcall module \n");

	//va = __get_free_page(GFP_KERNEL);
	va = malloc(1024);
	//va = kmalloc(256,GFP_KERNEL);
	//memset(va, 0, 256);
	strcpy(va, "start_trace:");
	strcat(va, tag);
	printf("BUF %s, VIRTUAL aDDRESS = %x \n",va, (unsigned int) va);
	//strcpy((char *)va, "K");
	//strcpy((char *)va, "V");
	//strcpy((char *)va, "M\0");
	//strcpy(va, "link\n");
	ret = vmmcall(rate,10, va,5,7,9);

	return ret;
}


void cleanup_module(void)
{
	//free_page(va);
	free(va);

}
void main(int argc, char ** argv)
{
	if(argc < 2) {
		printf("Must have at least one argument\n");
		exit(1);
	}
	if(argc == 2)
		init_module(argv[1], 10);
	else
		init_module(argv[1], atoi(argv[2]));
	cleanup_module();
}
